﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;
using System.Globalization;

namespace SharpGeo
{
    /// <summary>
    /// A lightweight immutable structure to store color values (ARGB) that is defined outside of any specific rendering technology
    /// </summary>
    public struct Color : IEquatable<Color>
    {
        #region Constructors

        /// <summary>
        /// Constructs a Color object
        /// </summary>
        /// <param name="alpha">The alpha component (0-255)</param>
        /// <param name="red">The red component (0-255)</param>
        /// <param name="green">The blue component (0-255)</param>
        /// <param name="blue">the green component (0-255)</param>
        public Color(byte alpha, byte red, byte green, byte blue)
        {
            _alpha = alpha;
            _red = red;
            _green = green;
            _blue = blue;
        }

        /// <summary>
        /// Constructs a Color object
        /// </summary>
        /// <param name="red">The red component (0-255)</param>
        /// <param name="green">The blue component (0-255)</param>
        /// <param name="blue">the green component (0-255)</param>
        public Color(byte red, byte green, byte blue)
            : this(0xff, red, blue, green)
        {
        }

        /// <summary>
        /// Constructs a Color object
        /// </summary>
        /// <param name="alpha">The alpha component (0-1)</param>
        /// <param name="red">The red component (0-1)</param>
        /// <param name="green">The blue component (0-1)</param>
        /// <param name="blue">the green component (0-1)</param>
        public Color(double alpha, double red, double green, double blue)
            : this(
            (byte)(alpha * 255),
            (byte)(red * 255),
            (byte)(green * 255),
            (byte)(blue * 255))
        {
        }

        /// <summary>
        /// Constructs a Color object
        /// </summary>
        /// <param name="red">The red component (0-1)</param>
        /// <param name="green">The blue component (0-1)</param>
        /// <param name="blue">the green component (0-1)</param>
        public Color(double red, double green, double blue)
            : this(
            255,
            (byte)(red * 255),
            (byte)(green * 255),
            (byte)(blue * 255))
        {
        }

        /// <summary>
        /// Constructs a color object
        /// </summary>
        /// <param name="value">A byte array packed alpha, red, blue, and gree</param>
        public Color(byte[] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value", Properties.Resources.ColorColorNullArgumentException);
            }

            if (value.Length != 4)
            {
                throw new ArgumentException(Properties.Resources.ColorColorArgumentException, "value");
            }

            _alpha = value[0];
            _red = value[1];
            _green = value[2];
            _blue = value[3];
        }

        /// <summary>
        /// Converts the Color to a four byte array
        /// </summary>
        /// <returns>A four byte array representing the color</returns>
        public byte[] ToByteArray()
        {
            return new byte[] { Alpha, Red, Green, Blue };
        }

        #endregion Constructors

        #region Private Fields

        private byte _alpha;
        private byte _blue;
        private byte _green;
        private byte _red;
        #endregion Private Fields

        #region Properties

        /// <summary>
        /// The Alpha component of the color (0 = transparent, 255 = opaque)
        /// </summary>
        public byte Alpha { get { return _alpha; } }

        /// <summary>
        /// The blue component of the color
        /// </summary>
        public byte Blue { get { return _blue; } }

        /// <summary>
        /// The Green component of the color
        /// </summary>
        public byte Green { get { return _green; } }

        /// <summary>
        /// The Red component of the color
        /// </summary>
        public byte Red { get { return _red; } }
        #endregion Properties

        #region Methods

        /// <summary>
        /// Compares two colors for inequality
        /// </summary>
        /// <param name="color1">The first color to compare</param>
        /// <param name="color2">The second color to compare</param>
        /// <returns>True if the colors are not equal</returns>
        public static bool operator !=(Color color1, Color color2)
        {
            return !color1.Equals(color2);
        }

        /// <summary>
        /// Compare two colors for equality
        /// </summary>
        /// <param name="color1">The first color to compare</param>
        /// <param name="color2">The second color to compare</param>
        /// <returns>True if the colors are equal</returns>
        public static bool operator ==(Color color1, Color color2)
        {
            return color1.Equals(color2);
        }

        /// <summary>
        /// Compares this Color with another color for equality
        /// </summary>
        /// <param name="other">The other color to compare</param>
        /// <returns>True if the colors are equal</returns>
        public bool Equals(Color other)
        {
            return _alpha == other._alpha && _red == other._red && _green == other._green && _blue == other._blue;
        }

        /// <summary>
        /// Compares this Color with another color for equality
        /// </summary>
        /// <param name="obj">The other color to compare</param>
        /// <returns>True if the colors are equal</returns>
        public override bool Equals(object obj)
        {
            if (!(obj is Color))
            {
                return false;
            }

            return Equals((Color)obj);
        }
        /// <summary>
        /// Gets the computed hashcode of this color
        /// </summary>
        /// <returns>The computed hashcode</returns>
        public override int GetHashCode()
        {
            unchecked
            {
                long l =
                   _alpha +
                   (37 * _red) +
                   (43 * _blue) +
                   (47 * _green);

                return (int)((l >> 32) ^ l);
            }
        }

        /// <summary>
        /// Returns a string representing this color
        /// </summary>
        /// <returns>The string representing this color</returns>
        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "ARGB({0},{1},{2},{3})", _alpha, _red, _green, _blue);
        }

        #endregion Methods
    }
}